package cn.handong.tool.algorithm.多线程;

import java.util.concurrent.*;
import java.util.function.Function;

public class TestThread {

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println("111");
        }).start();
    }

}

class TestExecutorService implements Runnable{

    String name;

    public TestExecutorService(String name){
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i < 6; i++) {
            System.out.println(this.name + i);
        }
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        TestExecutorService e = new TestExecutorService("E");
        TestExecutorService f = new TestExecutorService("F");
        TestExecutorService g = new TestExecutorService("G");
        TestExecutorService h = new TestExecutorService("H");
        TestExecutorService i = new TestExecutorService("I");
        pool.execute(e);
        pool.execute(f);
        pool.execute(g);
        pool.execute(h);
        pool.execute(i);
        pool.shutdown();
    }
}


class TestCallable implements Callable{

    private int ticket = 5;

    @Override
    public Object call() throws Exception {
        for (int i = 0; i < 5; i++) {
            if (this.ticket > 0){
                System.out.println("ticket="+ticket--);
            }
        }
        return "票卖完了";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable tc = new TestCallable();
        FutureTask<String> task = new FutureTask<String>(tc);
        new Thread(task).start();
        System.out.println(task.get());
    }
}